<template>
  <el-container class="home_con">
    <!-- 头部 -->
    <el-header>
      <div>
        <img
          src="../assets/heima.png"
          alt=""
        >
        <span>电商后台管理系统</span>
      </div>
      <el-button
        type="info"
        @click="remove"
      >退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏菜单 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 收缩按钮 -->
        <div
          class="toggle_button"
          @click="toggleFn"
        >|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse='isCollapse'
          :collapse-transition='false'
          router
          :default-active='activePath'
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index='item.id + ""'
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot='title'>
              <i :class="icons[item.id]"></i>
              <span style="margin-left:10px">{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + item1.path"
              v-for="item1 in item.children"
              :key="item1.id"
              @click="saveNavstate('/'+ item1.path)"
            >
              <template slot='title'>
                <i class="el-icon-menu"></i>
                <span>{{item1.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  data () {
    return {
      // 侧边栏菜单数据
      menulist: [],
      // 自定义字体图标
      icons: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 侧边栏是否收缩
      isCollapse: false,
      // 激活的链接地址
      activePath: window.sessionStorage.getItem('activePath')
    }
  },
  methods: {
    // 退出
    remove () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取侧边菜单数据
    async getMenus () {
      // 授权api要在请求头中添加token
      // const res = await this.$http.get('menus', { headers: { Authorization: window.sessionStorage.getItem('token') } })
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menulist = res.data
    },
    // 收缩侧边栏菜单
    toggleFn () {
      this.isCollapse = !this.isCollapse
    },
    // 把点击内容的链接地址存储的到本地,并激活
    saveNavstate (path) {
      window.sessionStorage.setItem('activePath', path)
      // this.activePath = path
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getMenus()
    // 浏览器刷新默认选择刷新前的选项
    // this.activePath = window.sessionStorage.getItem('activePath')
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.home_con {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  background-color: #373d41;
  color: #fff;
  font-size: 20px;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-menu {
  border: 0;
}
.toggle_button {
  background-color: #4a5064;
  line-height: 24px;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.el-main{
  background-color: #eaedf1;
}
</style>
